#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<unordered_map>
using namespace std;
int longestArithSeqLength(vector<int>& nums) {
    int n = nums.size();
    vector<vector<int>> dp(n, vector<int>(n, 2));
    unordered_map<int, int> hash;
    hash[nums[0]] = 0;

    int Max = 2;
    for (int i = 1; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int x = 2 * nums[i] - nums[j];
            if (hash.count(x)) {
                int k = hash[x];
                if (k < i) {
                    dp[i][j] = max(dp[i][j], dp[k][i] + 1);
                }
                Max = max(Max, dp[i][j]);
            }
        }
        hash[nums[i]] = i;
    }
    return Max;
}